using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Macaw.Moss2007.CustomErrorPageSolution
{
    public class CustomErrorPageFeature : SPFeatureReceiver
    {

        private void ApplyWebConfigModificationsToFarm(SPWebApplication webApp)
        {
            try
            {
                webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            }
            catch (System.UnauthorizedAccessException ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }

        private string Owner
        {
            get
            {
                return this.GetType().FullName + ", " + this.GetType().Assembly.FullName;
            }
        }

        private const string MODULENAME = "CustomErrorPageModule";

        private SPWebConfigModification AddCustomPageModule_modification
        {
            get
            {
                SPWebConfigModification modification = new SPWebConfigModification(string.Format("add[@name='{0}']", MODULENAME), "configuration/system.web/httpModules");
                modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                modification.Value = string.Format("<add name=\"{0}\" type=\"{1}\" />", MODULENAME, typeof(CustomErrorPageModule).AssemblyQualifiedName);
                modification.Owner = Owner;
                return modification;
            }
        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webapp = properties.Feature.Parent as SPWebApplication;
            if (webapp == null) return;

            webapp.WebConfigModifications.Add(AddCustomPageModule_modification);

            webapp.Update(); // Important. Otherwise webconfigmods can be removed upon activating other features
            ApplyWebConfigModificationsToFarm(webapp);
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webapp = properties.Feature.Parent as SPWebApplication;
            if (webapp == null) return;

            for (int idx = webapp.WebConfigModifications.Count - 1; idx >= 0; idx--)
            {
                try
                {
                    SPWebConfigModification conmod = webapp.WebConfigModifications[idx];
                    if (string.Compare(conmod.Owner, Owner, true) == 0)
                    {
                        webapp.WebConfigModifications.RemoveAt(idx);
                    }
                }
                catch (Exception ex)
                {
                }
            }

            webapp.Update(); // Important
            ApplyWebConfigModificationsToFarm(webapp);
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {

        }
    }
}
